/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.cms.portlet.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Element;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;
import com.x4ee.soa.portal.framework.core.FrameworkConfig;
import com.x4ee.soa.portal.framework.tools.PortletUploadFormParser;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-9-10下午10:06:23
 */
public class URIUploadActionFilter extends BaseRequestFilter implements ActionFilter {
	public static String getFileName(String fullname){
		String filename = fullname;
		if(filename.indexOf(File.separator)>-1){
			filename=filename.substring(filename.lastIndexOf(File.separator)+1,filename.length());
		}
		if(filename.indexOf("/")>-1){
			filename=filename.substring(filename.lastIndexOf("/")+1,filename.length());
		}
		if(filename.indexOf("\\")>-1){
			filename=filename.substring(filename.lastIndexOf("\\")+1,filename.length());
		}
		return filename;
	}
	private FrameworkConfigWraper tFrameworkConfigWraper =null;
	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {
		
		PortletUploadFormParser portletuploadFormParser = new PortletUploadFormParser(actionRequest,"UTF-8",0);
		String uriName = portletuploadFormParser.getParamValue("uriName");
		String dirName = portletuploadFormParser.getParamValue("dirName");

		Map fileMap = portletuploadFormParser.getFileMap();
		Set keySet = fileMap.keySet();

		if((keySet!=null)&&(!keySet.isEmpty())){
			Object archiveFileObj= fileMap.get(keySet.iterator().next());
			if((archiveFileObj!=null)&&(archiveFileObj instanceof FileItem)){
				FileItem archiveFile = (FileItem)archiveFileObj;
				String fileName = getFileName(archiveFile.getName());
				String portletAppPath = tFrameworkConfigWraper.getFrameworkConfig().getPortletContext().getRealPath("");
				File portletAppDir = new File(portletAppPath);
				if((portletAppDir!=null)&&(portletAppDir.exists())&&(portletAppDir.isDirectory())){
					File portletsDir = new File(portletAppDir,"portlets");
					if((portletsDir!=null)&&(portletsDir.exists())&&(portletsDir.isDirectory())){
						File portletDir = new File(portletsDir,tFrameworkConfigWraper.getFrameworkConfig().getPortletName());
						if((portletDir!=null)&&(portletDir.exists())&&(portletDir.isDirectory())){
							File targetDir = new File(portletDir,dirName);
							if((targetDir!=null)&&(targetDir.exists())&&(targetDir.isDirectory())){
								try{
									File targetFile = new File(targetDir,fileName);
									FileOutputStream targetFileOutputStream =  new FileOutputStream(targetFile);
									IOUtils.copy(archiveFile.getInputStream(),targetFileOutputStream);
									targetFileOutputStream.close();
									if(dirName.equals("jsp")){
										if(fileName.endsWith(".jsp")){
											tFrameworkConfigWraper.addUri(uriName,"jsp"+"/"+fileName);
											//URI Map
											Map frameworkUriMap = new HashMap();
											Map uriMap = FrameworkConfigWraper.getURIMapByRenderName(tFrameworkConfigWraper.getXmlConfig(),"ViewIndex");
											for(Iterator rIt = uriMap.keySet().iterator();rIt.hasNext();){
												Object keyObj = rIt.next();
												if((keyObj!=null)&&(keyObj instanceof String)){
													String key = (String)keyObj;
													Object valueObj = uriMap.get(keyObj);			
													if((valueObj!=null)&&(valueObj instanceof String)){
														String mValue = (String)valueObj;
														if(mValue.indexOf("jsp"+"/")>-1){
															String tValue= mValue.substring(mValue.lastIndexOf("jsp"+"/")+4, mValue.length());
															frameworkUriMap.put(key, tValue);
														}
													}
												}
											}
											actionRequest.getPortletSession().setAttribute(FrameworkConfigWraper.FRAMEWORK_URI_MAP,frameworkUriMap);
											return FrameworkConfigWraper.DEFAULT;
										}else{
											return FrameworkConfigWraper.UPLOAD;											
										}
									}else{
										return FrameworkConfigWraper.UPLOAD;
									}
								}catch(Exception e){
									e.printStackTrace();
									return FrameworkConfigWraper.ERROR;
								}
							}else{
								return FrameworkConfigWraper.ERROR;
							}
						}else{
							return FrameworkConfigWraper.ERROR;
						}
					}else{
						return FrameworkConfigWraper.ERROR;
					}
				}else{
					return FrameworkConfigWraper.ERROR;
				}
			}else{
				return FrameworkConfigWraper.ERROR;
			}
		}else{
			return FrameworkConfigWraper.ERROR;
		}
	}

	public void init(Element tXmlConfig, FrameworkConfig frameworkConfig) throws PortletException {
		super.init(tXmlConfig, frameworkConfig);
		tFrameworkConfigWraper = new FrameworkConfigWraper(tXmlConfig,frameworkConfig);		
	}
}